<?php

/**
 * This is the model class for table "b2b_ticket".
 *
 * The followings are the available columns in table 'b2b_ticket':
 * @property integer $id
 * @property string $activecode
 * @property integer $userid
 * @property integer $companyid
 * @property integer $groupid
 * @property string $cardno
 * @property string $realname
 * @property integer $orderstatus
 * @property string $examdate
 * @property string $startdate
 * @property string $enddate
 * @property string $create_at
 * @property string $update_at
 */
class B2bTicket extends CActiveRecord
{
    const UNACTIVATED = 1;
    const YUYUE_PROCESSING = 2;
    const YUYUE_CONFIRM = 3;
    const FINISHED = 4;
    const YUYUE_REFUSED = 5;
    
    const COMPANYUSER = 1;
    const GENERALUSER = 2;
    static $salt_code = "134532";//加盐密码
    /**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
         * @return B2bTicket the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'b2b_ticket';
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    "package" => array(self::BELONGS_TO, 'B2bPackage', array("packageid" => "id")),
                    "center" => array(self::BELONGS_TO, 'B2bSubCenter', array("centerid" => "id")),
                    "user" => array(self::BELONGS_TO, 'B2bUser',array("uid" => "id")),
                    "company" => array(self::BELONGS_TO, 'B2bCompany',array("companyid" => "id")),
		);
	}
        /**
         * $identity_no 证件号码
         * $realname 真实姓名
         * 
         */
        public function relationUserByCardNoName($uid,$identity_no,$realname){
            if(empty($identity_no)||empty($realname)){
                return false;
            }
            
            $criteria = new CDbCriteria;
            $criteria->addCondition("identity_no = '$identity_no'");
            $criteria->addCondition("realname = '$realname'");
            $criteria->addCondition("uid = 0");
            $criteria->addCondition("companyid != 0");
            try {
                $ret = $this->updateAll(array("uid"=>$uid), $criteria);
            } catch (Exception $exc) {
                echo $exc->getMessage();
                return false;
            }
            
            
            return $ret;
        }
        
        static function encActiveCode($id)
        {
            while(1){
                $now = mktime();
                mt_srand($now.$id);
                $no = sprintf("%06d", mt_rand(0,999999)).  substr(sprintf("%04d",$id),-4);

                $lenno = strlen($no);
                $saltlen = strlen(B2bTicket::$salt_code);

                for($i = 0;$i<$saltlen;$i++){
                    $t = $no[$i];
                    $no[$i] = $no[$lenno-B2bTicket::$salt_code[$i]]+B2bTicket::$salt_code[$i];
                    $no[$lenno-B2bTicket::$salt_code[$i]] = $t;
                }
                if(B2bTicket::model()->exists("activecode={$no}" === FALSE)){
                    break;
                }
            }
            return $no;
        }
        static function decActiveCode($id)
        {
            
        }

}